﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Print.Domain;
using Mengsk.ProxyDelivery.Service;
using Mengsk.ProxyDelivery.Utls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.Print.UI
{
    /// <summary>
    /// CountUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class GoodsCountUserControl : UserControl
    {
        private OrderGoodsService goodsCountRepertory = ServiceContainer.GetService<OrderGoodsService>();

        public GoodsCountUserControl()
        {
            InitializeComponent();
        }

        private void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var time = this.dpStart.Value == null ? DateTime.MinValue : this.dpStart.Value.Value;
                if (time <= DateTime.Now.AddDays(-60))
                {
                    throw new Exception("查询起始时间不能超过60天");
                }
                List<GoodsCount> counts = goodsCountRepertory.GetGoodsCount(1, 0, false, time, 0, 0).ToList();
                counts.Sort();//区
                counts.Sort();//连
                counts.Sort();//门
                counts.Sort();//街
                counts.Sort();//货号
                counts.Sort();//版本
                counts.Sort();//颜色
                counts.Sort();//尺码
                this.dgvGoodsCount.ItemsSource = counts.ToArray();
                this.tbTotalCount.Text = "共下载:" + counts.Select(obj => obj.Count).Sum() + "条数据";
                MessageBox.Show("下载成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("下载出错:" + ex.Message);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            var items = this.dgvGoodsCount.ItemsSource as Mengsk.ProxyDelivery.Domain.GoodsCount[];
            if (items == null || items.Length < 1)
            {
                MessageBox.Show("没有需要打印的数据");
                return;
            }

            try
            {
                string printer = LocalConfigService.GetValue("GoodsPrinter", "");
                if (string.IsNullOrWhiteSpace(printer))
                {
                    throw new Exception("请在系统配置里面，配置要使用的打印机");
                }
                string message = string.Format("是否打印:\n打印机:{0}\n打印数量:{1}", printer, items.Select(obj => obj.Count).Sum());
                if (MessageBox.Show(message, "提示", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                {
                    return;
                }
                var pd = PrintUtil.GetPrinter(printer);
                GoodsCountPrintDocument goodsCountDoc = new GoodsCountPrintDocument();
                goodsCountDoc.PageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
                goodsCountDoc.SetGoodsCount(items);
                pd.PrintDocument(goodsCountDoc, "拿货统计");
                MessageBox.Show("打印完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出错");
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.dpStart.Value = DateTime.Now.AddDays(-30);
        }

        private void btnPrintNew_Click(object sender, RoutedEventArgs e)
        {
            var items = this.dgvGoodsCount.ItemsSource as GoodsCount[];
            if (items == null || items.Length < 1)
            {
                MessageBox.Show("没有需要打印的数据");
                return;
            }

            try
            {
                string printer = LocalConfigService.GetValue("GoodsPrinter", "");
                if (string.IsNullOrWhiteSpace(printer))
                {
                    throw new Exception("请在系统配置里面，配置要使用的打印机");
                }

                //数据数量过滤
                int minCount = 1;
                int.TryParse(this.tbMinCount.Text.Trim(), out minCount);
                Dictionary<string, List<GoodsCount>> gcs = new Dictionary<string, List<GoodsCount>>();
                foreach (var gc in items)
                {
                    if (items.Count(obj => obj.Address == gc.Address) >= minCount)
                    {
                        if (gcs.ContainsKey(gc.Vendor) == false)
                        {
                            gcs[gc.Vendor] = new List<GoodsCount>();
                        }
                        gcs[gc.Vendor].Add(gc);
                    }
                }

                string message = string.Format("是否打印:\n打印机:{0}\n打印数量:{1}", printer, items.Select(obj => obj.Count).Sum());
                if (MessageBox.Show(message, "提示", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                {
                    return;
                }

                var pd = PrintUtil.GetPrinter(printer);
                GoodsCountPrintDocument2 goodsCountDoc = new GoodsCountPrintDocument2();
                goodsCountDoc.PageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
                goodsCountDoc.SetGoodsCount(gcs);
                pd.PrintDocument(goodsCountDoc, "拿货统计");
                MessageBox.Show("打印完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出错");
            }
        }
    }
}
